package com.zut.dao;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.zut.domain.OrderPractise;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrderPractiseDao {

    @Results(id = "groupWithOrderPractise",
            value = {
                    @Result(property = "id", column = "order_practise_id", id = true),
                    @Result(property = "practiseId", column = "practise_id"),
                    @Result(property = "userId", column = "user_id"),
                    @Result(property = "time", column = "finish_time"),
                    @Result(property = "status", column = "status")
            })
    @Select("select * from order_practise")
    List<OrderPractise> getAll();

    @Insert("insert into order_practise (practise_id,user_id,finish_time) " +
            "values(#{practiseId},#{userId},#{time})")
    int insert(OrderPractise orderPractise);

    @Delete("delete from order_practise where order_practise_id = #{id}")
    int delete(String id);

    @Update("<script>                                                           "
            + "update order_practise                                            "
            + "<set>                                                            "
            + "  <if test='practiseId != null'> practise_id=#{practiseId}, </if>"
            + "  <if test='userId != null'> user_id=#{userId},             </if>"
            + "  <if test='time != null'> finish_time=#{time},             </if>"
            + "  <if test='status != null'> status=#{status},              </if>"
            + "</set>                                                           "
            + "where order_practise_id = #{id}                                  "
            + "</script>                                                        ")
    int update(OrderPractise orderPractise);

    @Update("update order_practise set status = '0' where order_practise_id = #{id}")
    void updateStatus(String userId);

    @ResultMap("groupWithOrderPractise")
    @Select("select * from order_practise where status = '1'")
    List<OrderPractise> reserved();

    @Select("select order_practise_id as orderPractiseId, finish_time as finishTime,status,practise.practise_id as practiseId," +
            "practise_place as place,practise_subject as subject,interval_time as intervalTime" +
            " from order_practise,practise " +
            "where order_practise.practise_id = practise.practise_id and user_id = #{id} order by finish_time desc")
    List<Map<String,String>> getById(String id);

    @Select("select practise.practise_id as id," +
            "practise_place as place," +
            "practise_subject as subject," +
            "interval_time as time," +
            "if(order_practise_id is null,0,1) as status " +
            "from practise " +
            "left join order_practise on practise.practise_id = order_practise.practise_id and  order_practise.status = '1' " +
            "where practise.practise_place = #{place}")
    List<Map<String,String>> reservedMsg(String place);
}
